package com.wannengbang.cloudleader.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.BaseResponseBean;
import com.wannengbang.cloudleader.bean.ShopListBean;
import com.wannengbang.cloudleader.shop.ShopConfirmOrderActivity;
import com.wannengbang.cloudleader.shop.model.ShopModelImpl;
import com.wannengbang.cloudleader.utils.Dp2Px;
import com.wannengbang.cloudleader.utils.FastClickUtils;
import com.wannengbang.cloudleader.utils.GlideUtils;
import com.wannengbang.cloudleader.utils.ToastUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 *
 */
public class ShopCartDialog extends DialogFragment {


    @BindView(R.id.iv_picture)
    ImageView ivPicture;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.dialog_layout)
    LinearLayout dialogLayout;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_integral_price)
    TextView tvIntegralPrice;
    @BindView(R.id.tv_sort)
    TextView tvSort;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.tv_add)
    TextView tvAdd;
    private Dialog dialog;
    Unbinder unbinder;
    private Context mContext;
    private ShopListBean.DataBean.ItemListBean itemListBean;
    private int type;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initWindow();
        View view = inflater.inflate(R.layout.dialog_shop_cart, null);
        unbinder = ButterKnife.bind(this, view);
        mContext = getActivity();
        initView();
        return view;
    }

    public static ShopCartDialog newInstance(int type, ShopListBean.DataBean.ItemListBean itemListBean) {
        Bundle args = new Bundle();
        ShopCartDialog fragment = new ShopCartDialog();
        args.putSerializable("itemListBean", itemListBean);
        args.putInt("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    //    初始化请求
    private void initView() {
        type = getArguments().getInt("type", 0);
        itemListBean = (ShopListBean.DataBean.ItemListBean) getArguments().getSerializable("itemListBean");
        if (type == 2) {
            tvCommit.setText("立即兑换");
        } else {
            tvCommit.setText("确定");
        }

        GlideUtils.loadImage(mContext, ivPicture, itemListBean.getThumb());
        tvIntegralPrice.setText(itemListBean.getIntegral_price());
        tvTitle.setText(itemListBean.getName());

    }

    //设置window属性
    private void initWindow() {
        //去掉dialog的标题
        dialog = this.getDialog();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        Window window = dialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = (int) Dp2Px.dp2Px(500);
        lp.gravity = Gravity.BOTTOM;
        window.getAttributes().windowAnimations = R.style.commomDialog;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
    }


    @OnClick({R.id.iv_close, R.id.tv_commit, R.id.tv_sort, R.id.tv_add})
    public void onViewClicked(View view) {
        if (FastClickUtils.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.iv_close:
                dismiss();
                break;
            case R.id.tv_commit:
                if (type == 1) {
                    requestShopCartAdd(itemListBean.getId() + "", tvNumber.getText().toString());
                }
                if (type == 2) {
                    List<ShopListBean.DataBean.ItemListBean> beanList = new ArrayList<>();
                    itemListBean.setGoods_amount(Integer.parseInt(tvNumber.getText().toString()));
                    beanList.add(itemListBean);
                    Intent intent = new Intent(mContext, ShopConfirmOrderActivity.class);
                    intent.putExtra("itemList", (Serializable) beanList);
                    startActivity(intent);
                    dismiss();
                }

                break;
            case R.id.tv_sort:
                int num = Integer.parseInt(tvNumber.getText().toString());
                if (num > 1) {
                    num--;
                }
                tvNumber.setText(String.valueOf(num));
                break;
            case R.id.tv_add:
                num = Integer.parseInt(tvNumber.getText().toString());
                num++;
                tvNumber.setText(String.valueOf(num));
                break;
        }
    }

    public void requestShopCartAdd(String goods_id, String goods_amount) {
        new ShopModelImpl().requestIntegralShopCartAdd(goods_id, goods_amount, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort("添加成功");
                dismiss();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }
}
